from flask import jsonify, request, Blueprint
from flask_bcrypt import Bcrypt
from .models import user_roles, role_permissions, User, Role, Permission


bp = Blueprint('auth', __name__)

# API Endpoints
# @bp.route('/register', methods=['POST'])
# def register():
#     data = request.get_json()
#     username = data.get('username')
#     password = data.get('password')

#     if User.query.filter_by(username=username).first():
#         return jsonify({'message': 'User already exists'}), 400

#     hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
#     new_user = User(username=username, password=hashed_password)
#     db.session.add(new_user)
#     db.session.commit()
#     return jsonify({'message': 'User registered successfully'}), 201

# @bp.route('/login', methods=['POST'])
# def login():
#     data = request.get_json()
#     username = data.get('username')
#     password = data.get('password')

#     user = User.query.filter_by(username=username).first()
#     if not user or not bcrypt.check_password_hash(user.password, password):
#         return jsonify({'message': 'Invalid credentials'}), 401

#     access_token = create_access_token(identity=user.username, expires_delta=datetime.timedelta(days=1))
#     return jsonify({'access_token': access_token}), 200

# @bp.route('/assign_role', methods=['POST'])
# @jwt_required()
# def assign_role():
#     data = request.get_json()
#     username = data.get('username')
#     role_name = data.get('role')

#     user = User.query.filter_by(username=username).first()
#     role = Role.query.filter_by(name=role_name).first()

#     if not user or not role:
#         return jsonify({'message': 'User or Role not found'}), 404

#     user.roles.bpend(role)
#     db.session.commit()
#     return jsonify({'message': f'Role {role_name} assigned to user {username}'}), 200

# @bp.route('/protected', methods=['GET'])
# @jwt_required()
# def protected():
#     current_user = get_jwt_identity()
#     return jsonify({'message': f'Welcome {current_user}!'}), 200


@bp.route('/hello', methods=['GET'])
def hello():
    return jsonify({'message': f'Hello, AUTH!'}), 200